/**
 * 全局混入
 * @author：丁少华
 * @param：
 * @throws：
 * @time：2018/7/16
 */
import trucklogoImg from '../assets/img/driver-car.png'
import inforFacesImg from '../assets/img/infor-face.png'

import Vue from 'vue'
export default function() {
  Vue.mixin({
    data(){return {
        routerParams:{}
    }},
    methods: {
      // 表单校验
      async vldt(name, vlaue) {
        let result = await this.$validator.validate(name, vlaue)
        let msg = ''
        let errors = this.$validator.getErrors('phone').errors
        errors.forEach(function(item) {
          if (item.rule === name) {
            msg = item.msg
          }
        })
        let rep = {
          status: result,
          msg: msg
        }
        return rep
      },
      // 自定义toast
      toast(msg) {
        // Create toast
        this.toastCenter = this.$f7.toast.create({
          text: msg,
          position: 'center',
          closeTimeout: 2000
        })
        // Open toast
        this.toastCenter.open()
      },
      // 图片资源错误处理
      imgError(e) {
        let type = e.target.dataset.type.toString()
        let maps = {
          truck_logo: trucklogoImg,
          infor_faces: inforFacesImg
        }
        e.target.src = maps[type]
      },
      goBack(param){
        this.$f7.router.back(param)
      },
      go(url,param){
        let fullUrl=url
        if(param){
            let paramStr=JSON.stringify(param);
            fullUrl=url+paramStr;
        }
         this.$f7router.navigate(fullUrl)
      },
      getParam(){
        if(this.$f7Route && this.$f7Route.params.param){
            this.routerParams=JSON.parse(this.$f7Route.params.param)
        }
      }
    },
    created(){
        this.getParam()
    }
  })
}
